$(function() {
	hideAllTables();

	var firstTab = $('.mytabs').eq(0);
	var tableId = firstTab.find('a').attr('href');
	var table = $(tableId);
	table.css({'display': 'block'});

	$('a').on('click', onLinkClicked);
	$('a').eq(0).click();
	$(window).on('resize', onWindowResize);
	$(window).on('resize', resizeColumns);
    adjustSizes();
    setDocMinSize();
})

function adjustSizes() {
	setTableViewportSize();
	resizeColumns();
}

function setDocMinSize() {
    var tabs = $('.tab');

    // set the tabs to the max equal width
    var maxTabWidth = tabs.eq(0).width();
    var tabCount = tabs.length;

    if (tabCount > 1) {
        for (var i = 1; i < tabs.length; i++) {
            var tab = tabs.eq(i);
            if (maxTabWidth < tab.width()) {
                maxTabWidth = tab.width();
            }
        }
    }

    tabs.css({'width': maxTabWidth + 'px'});
    $('html body').css({'min-width': maxTabWidth * tabCount + 'px'});
}

function setTableViewportSize() {
	var viewportHeight = $(window).height();
	var tableHeight = viewportHeight - 100; // top padding + tab header	
	var scrollContents = $('.scrollContent');
	for (var i = 0; i < scrollContents.length; i++) {
		var sc = scrollContents.eq(i);
		if (sc.height() > tableHeight) {
			sc.css({'height': tableHeight + 'px'});
		}
	}
}

function onLinkClicked(event) {
	hideAllTables();

	var tableId = $(this).attr('href');
	showTable(tableId);

	$(this).css({'text-decoration': 'none'});

	var tabs = $('.tab');
    tabs.css({'background-color': '#0D8ECE',
              'background-image': '-webkit-gradient(linear, left top, left bottom, from(#0D8ECE), to(#0071BE))',
              'background-image': '-webkit-linear-gradient(top, #0D8ECE, #0071BE)',
              'background-image': '   -moz-linear-gradient(top, #0D8ECE, #0071BE)',
              'background-image': '   -ms-linear-gradient(top, #0D8ECE, #0071BE)',
              'background-image': '     -o-linear-gradient(top, #0D8ECE, #0071BE)',
              'background-image': '       linear-gradient(top, #0D8ECE, #0071BE)',
              'border': '1px solid #114F8E',
              'text-align': 'center',
              'color': 'white',
              'border-bottom': '2px solid #114F8E',
              'font-weight': 'bold'});

	var linkWrapper = $(this).children('.tab');
    linkWrapper.css({'background-color': '#ebf3fc',
                     'background-image': '-webkit-gradient(linear, left top, left bottom, from(#ebf3fc), to(#dce9f9))',
                     'background-image': '-webkit-linear-gradient(top, #ebf3fc, #dce9f9)',
                     'background-image': '   -moz-linear-gradient(top, #ebf3fc, #dce9f9)',
                     'background-image': '   -ms-linear-gradient(top, #ebf3fc, #dce9f9)',
                     'background-image': '     -o-linear-gradient(top, #ebf3fc, #dce9f9)',
                     'background-image': '       linear-gradient(top, #ebf3fc, #dce9f9)',
                     'border': '1px solid #dce9f9',
                     'color': 'black',
                     'border-bottom': '2px solid #dce9f9',
                     'font-weight': 'bold'});
}

function onWindowResize() {
	setTableViewportSize();
}

function hideAllTables() {
	$('.mytabs').css({'display': 'none'});
}

function showTable(tableId) {
	var tableWrapper = $(tableId);
	// tableWrapper.fadeIn('fast');
	tableWrapper.css({'display': 'block'});
	event.preventDefault();
	setTableViewportSize();
	resizeColumns();
}

function resizeColumns() {

	var scrollContents = $('.scrollContent');
	
	for (var i = 0; i < scrollContents.length; i++ ) {
		
		const TailWidth = 16;
		var scrollContent = scrollContents.eq(i);
		var cells = scrollContent.find('td');
		var tableObj = scrollContent.parent();
		var columnCount = tableObj.find('th').length;
		var columnWidth = 100;
		var headerContentWidth;
        var horHeader = tableObj.find('thead.fixedHeader tr');

		if (scrollContent.hasScrollBar()) {

			headerContentWidth = horHeader.width() - TailWidth;
			columnWidth = headerContentWidth / columnCount;

			var sections = horHeader.find('th');
			for (var sectionIndex = 0; sectionIndex < sections.length; sectionIndex++) {
				if (sectionIndex == (sections.length - 1)) {
					sections.eq(sectionIndex).css({'width': (columnWidth + TailWidth) + 'px'});
				} else {
					sections.eq(sectionIndex).css({'width': columnWidth + 'px'});
				}
			}

		} else {
			headerContentWidth = horHeader.width();
			columnWidth = headerContentWidth / columnCount;
			var headers = tableObj.find('.fixedHeader th');
			headers.css({'width': columnWidth + 'px'});
		}
		cells.css({'width': columnWidth + 'px'});
	}
}

(function($) {
	$.fn.hasScrollBar = function() {
		return this.get(0).scrollHeight > this.height();
	}
})(jQuery);
